package navigation;

/**
 *
 * @author hyurah
 */

public class Field {

	private FieldDirectionState[] directions;
	private boolean known;
	private boolean deadEnd;

	public Field() {
		known = false;
		deadEnd = false;
		directions = new FieldDirectionState[4];
		for(int i=0; i<directions.length; i++) {
			directions[i]=FieldDirectionState.UNKNOWN;
		}
	}

	public boolean isKnown() {
		return known;
	}

	public boolean isDeadEnd() {
		return deadEnd;
	}

	public boolean isDeadEnd(int direction) {
		return deadEnd && directions[direction]==FieldDirectionState.PATH;
	}

	public void setField(boolean north, boolean east, boolean south, boolean west) {
		known = true;
		if(north) setPathFound(Direction.NORTH);
		else setEmptyFound(Direction.NORTH);
		if(east) setPathFound(Direction.EAST);
		else setEmptyFound(Direction.EAST);
		if(south) setPathFound(Direction.SOUTH);
		else setEmptyFound(Direction.SOUTH);
		if(west) setPathFound(Direction.WEST);
		else setEmptyFound(Direction.WEST);
	}
	
	public void setPathFound(int direction) {
		if(directions[direction]==FieldDirectionState.UNKNOWN)
			directions[direction]=FieldDirectionState.PATH;
		if(!known) {
			checkUnknown();
		}
	}

	public void setEmptyFound(int direction) {
		if(directions[direction]==FieldDirectionState.UNKNOWN)
			directions[direction]=FieldDirectionState.EMPTY;
		if(!known) {
			checkUnknown();
		}
	}

	public void setObjectFound(int direction) {
		directions[direction]=FieldDirectionState.OBJECT;
	}

	public void setDeadEndFound(int direction) {
		known = true;
		deadEnd = true;
		for(int i=0; i<directions.length; i++) {
			directions[i]=FieldDirectionState.EMPTY;
		}
		directions[direction]=FieldDirectionState.PATH;
	}

	public FieldDirectionState getFieldDirectionState(int direction) {
		return directions[direction];
	}

	private void checkUnknown() {
		int z=0;
		for(int i=0; i<4; i++) {
			if(directions[i]==FieldDirectionState.UNKNOWN) z++;
		}
		if(z==0) known = true;
	}

}
